package com.campus.dao.impl;

import com.campus.dao.UserDao;
import com.campus.model.Teacher;
import com.campus.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
@Slf4j
public class UserDaoImpl implements UserDao {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public Integer insert(User user) {
        String sql=" INSERT INTO t_sys_user (id,login_name,password,gender,birthday,phone,email,status) VALUES " +
                " (?,?,?,?,?,?,?,?)";
        Object[] args = new Object[]{
                user.getUser_id(),
                user.getName(),
                user.getPassword(),
                user.getGender(),
                user.getBirthday(),
                user.getPhone(),
                user.getEmail(),
                user.getStatus(),
        };
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public List<User> queryUserByName(String name) {
        String sql = " SELECT * FROM t_sys_user WHERE login_name LIKE ?";
        return jdbcTemplate.query(sql, new Object[]{"%" + name + "%"}, new RowMapper<User>() {
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setUser_id(rs.getInt("id"));
                user.setName(rs.getString("login_name"));
                user.setGender(rs.getString("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setPhone(rs.getString("phone"));
                user.setEmail(rs.getString("email"));
                return user;
            }
        });
    }
}
